package br.com.winesys.geta.databinding.component.table.renderers;

import java.awt.Component;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

public class NumberTableCellRenderer extends NoBorderTableCellRenderer {

  private NumberFormat numberFormat;

  public NumberTableCellRenderer(boolean groupingUsed, int precision) {
    this.numberFormat = NumberFormat.getInstance();
    this.numberFormat.setGroupingUsed(groupingUsed);
    this.numberFormat.setMaximumFractionDigits(precision);
    this.numberFormat.setMinimumFractionDigits(precision);
  }

  public NumberTableCellRenderer(String format) {
    this.numberFormat = new DecimalFormat(format);
  }

  public NumberTableCellRenderer(NumberFormat numberFormat) {
    this.numberFormat = numberFormat;
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, (value == null) ? "" : this.numberFormat.format(value), isSelected, hasFocus, row, column);
    renderer.setHorizontalAlignment(SwingConstants.RIGHT);
    return renderer;
  }
}
